﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Billing.DataObjects;
using Billing.WindowsBase;

namespace Billing.Win
{
    public partial class InvoiceForm : Billing.WindowsBase.BaseEditor
    {
        public InvoiceForm(eOpenMode OpenMode)
            : base(OpenMode)
        {
            this.OpenMode = OpenMode;
            InitializeComponent();
        }
        BillingDataContext mBillingDataContext;
        public BillingDataContext BillingDataContext
        {
            get
            {
                if (mBillingDataContext == null)
                    mBillingDataContext = new BillingDataContext();
                return mBillingDataContext;
            }
            set { mBillingDataContext = value; }
        }
        ClientsView InvoiceClient = null;
        private void trvClient_ChooseBootonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            Billing.Win.ClientsSelectionForm frm = new Billing.Win.ClientsSelectionForm(false);

            frm.AllowSelect = true;


            if (frm.ShowDialog() == DialogResult.OK)
            {
                ClientsView cv = (ClientsView)frm.SelectedObjects[0].CurrentObject;

                if (cv.ClientTypeID == 2) //isJuridical
                {
                    InvoiceClient = cv;
                    this.trvClient.BaseObject = InvoiceClient;
                    this.txtTaxCode.Text = InvoiceClient.IdentificationNumber;
                    this.txtAddress.Text = InvoiceClient.Address;
                }
                else
                    MessageBox.Show("ფიზიკურ პირზე ოპერაცია დაუშვებელია!");
            }
        }
        long? InvoiceKey;
        protected override void onSave()
        {
            try
            {
                this.BillingDataContext.InvoiceManualAdd(this.dtDocumentDate.DateTime, (decimal)this.txtAmount.Value, InvoiceClient.ID, InvoiceClient.Name, InvoiceClient.Address, InvoiceClient.IdentificationNumber, ref InvoiceKey);
                this.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }
    }
}
